/**
 * 
 */
package com.zxc.thread;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.Vector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


/**
 * @author admin
 *
 */
public class TaskPoolUtil {

	private static ExecutorService gdsThreadExecutor = Executors.newFixedThreadPool(10);
	
	public static void pushGdsLogTask(Runnable task){
		gdsThreadExecutor.submit(task);
	}
	
	
	public static void main(String[] args) {
		
		Vector<Long> v = new Vector<>();
		
//		String content = "{\"cid\":\"test20170707\",\"fromCity\":\"HKG\",\"toCity\":\"LON\",\"fromDate\":\"20171009\",\"retDate\":\"20171109\",\"tripType\":\"2\",\"adultNum\":1,\"childNum\":1}";
		String content = "{\"guid\":193202736152359100,\"cachehash\":\"1354717650\",\"dss\":\"AMAD_F\",\"ucode\":\"\",\"cid\":\"\",\"fromcity\":\"BUE\",\"tocity\":\"BRC\",\"adult\":\"1\",\"child\":\"0\",\"flightoption\":\"oneway\",\"starttime\":\"2021-12-24\",\"endtime\":\"\",\"fromto\":\"BUE-BRC\",\"flightClass\":\"Economy\",\"ipcc\":\"BUEG123S8\",\"tradeId\":\"101\",\"qtegoodcodes\":\"\"}";
		List<Integer> testlist = new Vector<>();
		List<Integer> connecttimeout = new Vector<>();
		List<Integer> readtimeout = new Vector<>();
		for (int i = 0; i < 1000; i++) {
			
			pushGdsLogTask(new Runnable() {
				
				@Override
				public void run() {
					
					long t1 = System.currentTimeMillis();
					
					// 服务地址
					URL url = null;
					try {
//						url = new URL("http://192.168.18.100:53080/tripQuery/query");
						url = new URL("http://47.88.55.122:9999/amadeusQuery?d="+System.currentTimeMillis());
//						url = new URL("http://ticketgj.china-333.com/intequerysanhe.web");
					} catch (MalformedURLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					// 设定连接的相关参数
					HttpURLConnection connection = null;
					String strResponse = "";
					BufferedReader reader = null;
					try {
						connection = (HttpURLConnection) url.openConnection();
						connection.setConnectTimeout(30 * 1000);
						connection.setReadTimeout(30 * 1000);
						connection.setDoOutput(true);
						connection.setRequestMethod("POST");
						connection.setRequestProperty("Content-Type",
								"application/json; charset=utf-8");
						connection.setRequestProperty("Connection", "close");
						connection.setRequestProperty("Accept-Encoding", "gzip, deflate");
						
						// 输出
						OutputStreamWriter output = new OutputStreamWriter(
								connection.getOutputStream());
						output.write(content);
						output.flush();
						output.close();

						// 获取服务端的反馈
						String strLine = "";
						int statusCode = connection.getResponseCode();
						if (statusCode == 200) {
							InputStream in = connection.getInputStream();
				
							reader = new BufferedReader(new InputStreamReader(in,
									"UTF-8"));
							while ((strLine = reader.readLine()) != null) {
								strResponse += strLine + "\n";
							}
							strResponse=strResponse.trim();
						}
						
						System.out.println("status:"+statusCode+",data-len:"+strResponse.length());
						testlist.add(1);
					} catch (SocketTimeoutException e) {
						if(e.getMessage().contains("Connection")){
							connecttimeout.add(1);
						}
						if(e.getMessage().contains("Read")){
							readtimeout.add(1);
						}
						e.printStackTrace();
					} catch (Exception e) {
						if(e.getMessage().contains("Connection")){
							connecttimeout.add(1);
						}
						if(e.getMessage().contains("Read")){
							readtimeout.add(1);
						}
						e.printStackTrace();
					} finally {
						try {
							if (reader != null) {
								reader.close();
							}
						} catch (Exception e) {
						}
						try {
							connection.disconnect();
						} catch (Exception e) {
						}
					}
					
					v.add(System.currentTimeMillis()-t1);
				}
			});
			
			if(i >= 100){
				try {
					Thread.sleep(50);
				} catch (InterruptedException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
			}else{
				try {
					Thread.sleep(10);
				} catch (InterruptedException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
			}
		}
		
		gdsThreadExecutor.shutdown();
		while (true) {
			if(gdsThreadExecutor.isTerminated()){
				System.out.println("连接超时数:"+connecttimeout.size());
				System.out.println("响应超时数:"+readtimeout.size());
				System.out.println("成功数:"+testlist.size());
				//System.out.println(v.toString());
				System.out.println("完成了");
				break;
			}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
